package com.hss.ndknokillservice;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import com.hss.ndknokillservice.aidl.IMyAidlInterface;

/**
 * <b>Project:</b> NDKNoKillService<br>
 * <b>Create Date:</b> 2016/10/18 0018<br>
 * <b>Author:</b> huang saisai <br>
 * <b>Description:</b> <br>
 */
public class RemoteService extends Service {
    MyBinder myBinder;
    SerCon serCon;

    @Override
    public void onCreate() {
        super.onCreate();
        myBinder = new MyBinder();
        if (null == serCon){
            serCon = new SerCon();
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        bindService(new Intent(RemoteService.this, LocalService.class), serCon, Context.BIND_IMPORTANT);
        Toast.makeText(RemoteService.this, "RemoteService 启动", Toast.LENGTH_LONG).show();

    }

    class MyBinder extends IMyAidlInterface.Stub{

        @Override
        public String getServiceName() throws RemoteException {
            return "RemoteService";
        }
    }

    class SerCon implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.e("info", "链接本地成功");

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Toast.makeText(RemoteService.this, "链接本地成异常", Toast.LENGTH_LONG).show();
            startService(new Intent(RemoteService.this,LocalService.class));
            bindService(new Intent(RemoteService.this, LocalService.class), serCon, Context.BIND_IMPORTANT);

        }
    }
}
